/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mycompany.form;

import dao.HinhThucKhuyenMai1DAO;
import dao.MatHangDAO;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import pojo.Hinhthuckhuyenmai1;
import pojo.Mathang;

/**
 *
 * @author dell pro
 */
public class QuanLyCacChuongTrinh extends javax.swing.JFrame {

    /**
     * Creates new form QuanLyCacChuongTrinh
     */
    
    private DefaultComboBoxModel cbMatHang1;
    private DefaultComboBoxModel cbMatHang2;
    private DefaultComboBoxModel cbMatHang3;
    
    private int flag1 = 0;
    private int flag2 = 0;
    
    List<Mathang> listMatHang1 = new ArrayList<Mathang>();
    List<Mathang> listMatHang2 = new ArrayList<Mathang>();
    List<Mathang> listMatHang3 = new ArrayList<Mathang>();
    
    public QuanLyCacChuongTrinh() {
        initComponents();
        cbMatHang1 = new DefaultComboBoxModel();
        cbMatHang2 = new DefaultComboBoxModel();
        cbMatHang3 = new DefaultComboBoxModel();
        
        jComboBox1.setModel(cbMatHang1);
        //jComboBox2.setModel(cbMatHang2);
        
        listMatHang1 = MatHangDAO.layDanhSachMatHang();
        listMatHang2 = MatHangDAO.layDanhSachMatHang();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel15 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("QUẢN LÝ GIÁ BÁN");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        jLabel2.setText("MÃ SẢN PHẨM");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        jLabel3.setText("TÊN SẢN PHẨM");

        jLabel4.setText("jLabel4");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        jLabel5.setText("GIÁ BÁN");

        jButton1.setText("ĐỒNG Ý THAY ĐỔI");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 51, 51));

        jButton3.setText("LƯU THIẾT LẬP");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("ĐÓNG");

        jLabel15.setForeground(new java.awt.Color(255, 0, 51));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel15)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5))
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4))
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1))))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4)))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addComponent(jButton1)
                        .addGap(12, 12, 12))
                    .addComponent(jLabel15))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(50, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
         for (int i=0;i<listMatHang1.size();i++){
            cbMatHang1.addElement(listMatHang1.get(i).getIdmathang());
        }
        
        for (int i=0;i<listMatHang2.size();i++){
            cbMatHang2.addElement(listMatHang2.get(i).getIdmathang());
        }
    }//GEN-LAST:event_formWindowActivated

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
         String maSP1 = (String)jComboBox1.getSelectedItem();
         for (int i=0;i< listMatHang1.size();i++){
            if (maSP1.equals(listMatHang1.get(i).getIdmathang())){
                jLabel4.setText(listMatHang1.get(i).getTenmathang());
            }
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        flag1 = 1;
        if(!jTextField1.getText().equals("")){
            jLabel6.setText("Dữ liệu được đồng ý thay đổi");
        }
        else
        {
            jLabel6.setText("Bạn phải nhập giá bán mới nếu muốn thay đổi");
            flag1 = 0;
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
//        if(flag1 == 1 && flag2 == 1){
//            Mathang mathang1 = new Mathang();
//            Hinhthuckhuyenmai1 hinhthuckhuyenmai1 = new Hinhthuckhuyenmai1();
//            
//            mathang1.setIdmathang(jComboBox1.getSelectedItem().toString());
//            mathang1.setTenmathang(jLabel4.getText());
//            String makhuyenmai = HinhThucKhuyenMai1DAO.MaTangTuDong();
//            hinhthuckhuyenmai1.setIdhinhthuckhuyenmai1(makhuyenmai);
//            hinhthuckhuyenmai1.setMachuongtrinhkhuyenmai(makhuyenmai);
//            int soluong = 0;
//             if(!jTextField2.getText().equals("")){
//                soluong = Integer.parseInt(jTextField2.getText()); 
//             }
//             else
//             {
//                 soluong = 1;
//             }
//            hinhthuckhuyenmai1.setSoluongmua(soluong);
//            int phantramgiam = Integer.parseInt(jTextField3.getText());
//            hinhthuckhuyenmai1.setGiamgia(phantramgiam);
//            int giaxuat = Integer.parseInt(jTextField1.getText());
//            //giaxuat = (giaxuat * phantramgiam) / 100;
//            mathang1.setGiaxuat(giaxuat);
//            boolean update = MatHangDAO.capNhatMatHang(mathang1);
//            boolean updateKhuyenMai = HinhThucKhuyenMai1DAO.capNhatMatHang(hinhthuckhuyenmai1);
//            if(update && updateKhuyenMai){
//                 JOptionPane.showConfirmDialog(null,"Dữ liệu thay đổi thành công");
//            }
//            else
//            {
//                  JOptionPane.showConfirmDialog(null,"Dữ liệu thay đổi không thành công");
//            }
//        }
//        
//        if(flag1 == 1 && flag2 == 0){
//            Mathang mathang1 = new Mathang();
//            mathang1.setIdmathang(jComboBox1.getSelectedItem().toString());
//            mathang1.setTenmathang(jLabel4.getText());
//             int giaxuat = Integer.parseInt(jTextField1.getText());
//            //giaxuat = (giaxuat * phantramgiam) / 100;
//            mathang1.setGiaxuat(giaxuat);
//            boolean update = MatHangDAO.capNhatMatHang(mathang1);
//            if(update){
//                 JOptionPane.showConfirmDialog(null,"Dữ liệu thay đổi thành công");
//            }
//            else
//            {
//                  JOptionPane.showConfirmDialog(null,"Dữ liệu thay đổi không thành công");
//            }
//        }
//        
//        if(flag2 == 1 && flag1 == 0){
//             Hinhthuckhuyenmai1 hinhthuckhuyenmai1 = new Hinhthuckhuyenmai1();
//             String makhuyenmai = HinhThucKhuyenMai1DAO.MaTangTuDong();
//             hinhthuckhuyenmai1.setIdhinhthuckhuyenmai1(makhuyenmai);
//             hinhthuckhuyenmai1.setMachuongtrinhkhuyenmai(makhuyenmai);
//             int soluong = 0;
//             if(!jTextField2.getText().equals("")){
//                soluong = Integer.parseInt(jTextField2.getText()); 
//             }
//             else
//             {
//                 soluong = 1;
//             }
//            hinhthuckhuyenmai1.setSoluongmua(soluong);
//            int phantramgiam = Integer.parseInt(jTextField3.getText());
//            hinhthuckhuyenmai1.setGiamgia(phantramgiam);
//            boolean updateKhuyenMai = HinhThucKhuyenMai1DAO.capNhatMatHang(hinhthuckhuyenmai1);
//            if(updateKhuyenMai){
//                 JOptionPane.showConfirmDialog(null,"Dữ liệu thay đổi thành công");
//            }
//            else
//            {
//                  JOptionPane.showConfirmDialog(null,"Dữ liệu thay đổi không thành công");
//            }
//            
//        }
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(QuanLyCacChuongTrinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(QuanLyCacChuongTrinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(QuanLyCacChuongTrinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(QuanLyCacChuongTrinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new QuanLyCacChuongTrinh().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
